package com.qingxin.park.service;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import com.qingxin.park.entity.ParkTeSe;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.UploadFile;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;

/**
 * 特色基地
 * @author Administrator
 *
 */
@Service
public class FarmParkTeseService extends BaseService<ParkTeSe, String> {
	
	/**
	 * 
	 * @param parkid
	 * @param tstype 1资质认证2管理制度3特色产品
	 * @return
	 */
	public ParkTeSe getParkTeSe(String parkid, String tstype){
		ParkTeSe parkTeSe = (ParkTeSe) getByHql("from ParkTeSe where parkid=? and tstype=?", parkid, tstype);
		return parkTeSe;
	}
	
	
	
	/**
	 * 特色基地 列表信息
	 * @param leftdata
	 * @param type
	 * @param pageParam
	 * @param loginUser
	 * @return
	 */
	public Pager list(String type,String leftdata, String title,PageParam pageParam, LoginUser loginUser){
		
		Querier querier = Querier.create("select new com.qingxin.park.entity.ParkTeSe(" +
			"p.parktsid, p.parkid, p.title, p.tstype, p.fujian, p.create_by, p.create_time,f.parkName)" +
			" from ParkTeSe p ,FarmPark f where p.parkid = f.parkid and p.parkid=:parkid and p.tstype="+type);
		querier.setParam("parkid", leftdata);
		if(StringUtils.isNotEmpty(title)){
			title = title.replace("'", "");
			querier.append(" and (p.title like :title) ");
			querier.setParam("title", "%"+title+"%");
		}
		querier.append(pageParam.getOrderby(" order by p.create_time asc","p"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}

	/**
	 * 
	 * @param parkTeSe
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(ParkTeSe parkTeSe, LoginUser loginUser,
			HttpServletRequest request) {
		// TODO Auto-generated method stub
		Date now = new Date();
		parkTeSe.setCreate_time(now);
		parkTeSe.setCreate_by(loginUser.getLogin_name());
	
		if(StringUtils.isNotEmpty(parkTeSe.getFujian())){
			UploadFile.moveTmpTo(parkTeSe.getFujian(),UploadFile.DIR_PARK_FUJIAN);
		}
		this.save(parkTeSe);
		return null;
	}
	
	/**
	 * 删除
	 * @param parkteid
	 * @param request
	 * @return
	 */
	public SysError delete(String[] parkteid, HttpServletRequest request){
		for(String id : parkteid){
			ParkTeSe parkTeSe = this.get(id);
			if(parkTeSe == null){
				continue;
			}
			this.delete(parkTeSe);
		}
		return null;
	}
	
	/**
	 * 更新
	 * @param parkTeSe
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(ParkTeSe parkTeSe, LoginUser loginUser, HttpServletRequest request){
		ParkTeSe old = getSafeEntity(parkTeSe.getParktsid());
		
		if(StringUtils.isNotEmpty(parkTeSe.getFujian()) && !parkTeSe.getFujian().equals(old.getFujian())){
			UploadFile.moveTmpTo(parkTeSe.getFujian(),UploadFile.DIR_PARK_FUJIAN);
		}
		
		String fields = "title,tsdescrib,fujian";
		BeanUtil.setTargetFields(old, parkTeSe, fields, true);
		Updater<ParkTeSe> updater = new Updater<>(old, fields);
		this.update(updater);		
		return null;
	}
	
	
}
